﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Convertor.Plugin
{
    /// <summary>
    /// 
    /// </summary>
    public class Language
    {
        /// <summary>
        /// Change UI Language
        /// </summary>
        /// <param name="cultrueName"></param>
        public static void Change(string cultrueName = null)
        {
            if (cultrueName != null)
            {
                CultureInfo cultrue = new CultureInfo(cultrueName);
                System.Threading.Thread.CurrentThread.CurrentUICulture = cultrue;
            }
            List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
            foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
            {
                dictionaryList.Add(dictionary);
            }


            string requestedCulture = string.Format(@"Assets\Lang\StringResource.{0}.xaml", System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName);
            var resourceDictionarys = dictionaryList.Where(p => p.Source.OriginalString.EndsWith(requestedCulture)).ToList();
            foreach (var resourceDictionary in resourceDictionarys)
            {
                Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }
    }
}
